package lxt.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import lxt.dao.BorrowTruckDao;
import lxt.dao.ChargeRecordDao;
import lxt.dao.DriverDao;
import lxt.dao.DriverWorkloadDao;
import lxt.dao.InvoiceInfoDao;
import lxt.dao.InvoiceOrderDao;
import lxt.entity.BorrowTruckInfo;
import lxt.entity.ChargeRecord;
import lxt.entity.DriverInfo;
import lxt.entity.DriverWorkload;
import lxt.entity.DriverWorkloadId;
import lxt.entity.InvoiceInfo;
import lxt.entity.InvoiceOrder;
import lxt.service.InvoiceOrderService;
/**
 * 订单service
 * @author lit
 *
 */
@SuppressWarnings("unchecked")
public class InvoiceOrderServiceImpl implements InvoiceOrderService {
	private InvoiceOrderDao invoiceOrderDao ; //订单
	private ChargeRecordDao chargeRecordDao; //运费
	private InvoiceInfoDao invoiceInfoDao;//运输方案
	private DriverDao driverDao; //驾驶员
	private DriverWorkloadDao driverWorkloadDao ; //自己车
	private BorrowTruckDao borrowTruckDao; //借车表
	/**
	 * 加载所有信息
	 */
	public List<InvoiceOrder> loadAll() throws Exception{
		return (List<InvoiceOrder>) this.invoiceOrderDao.loadAll(InvoiceOrder.class);
	}
	/**
	 * 分页查询所有订单
	 * @param orderTime 订单日期
	 * @param ClientNameS 发货单位
	 * @param ClientNameR 收货单位
	 * @param offset
	 * @param pageSize
	 * @return
	 */
	public List<InvoiceOrder> loadByPage(String orderTime, String ClientNameS,
			String ClientNameR, int offset, Integer pageSize) throws Exception{
		return invoiceOrderDao.loadByPage(orderTime, ClientNameS, ClientNameR, offset, pageSize);
	}
	/**
	 * 根据分页条件查询订单条数
	 * @param orderTime 订单日期
	 * @param ClientNameS 发货单位
	 * @param ClientNameR 收货单位
	 * @return
	 */
	public Integer loadOrderCount(String orderTime, String ClientNameS,String ClientNameR) throws Exception{
		return invoiceOrderDao.loadOrderCount(orderTime, ClientNameS, ClientNameR);
	}
	/**
	 * 添加订单
	 * @param charge
	 * @return
	 * @throws Exception
	 */
	public boolean addInvoiceOrder(InvoiceOrder invoiceOrder,ArrayList<InvoiceInfo> InvoiceInfos,
			ChargeRecord chargeRecordS,ChargeRecord chargeRecordR,ArrayList<String> drivers,ArrayList<BorrowTruckInfo> borrowTrucks)throws Exception{
		/**
		 * 插入订单表
		 */
		this.invoiceOrderDao.save(invoiceOrder);
		/**
		 * 插入chargeRecord表
		 */
		if(chargeRecordS!=null){
			this.chargeRecordDao.save(chargeRecordS);
		}
		if(chargeRecordR!=null){
			this.chargeRecordDao.save(chargeRecordR);
		}
		/**
		 * 插入InvoiceInfo表
		 */
		Serializable id;
		ArrayList<Serializable> myids = new ArrayList<Serializable>(); //自己的车
		ArrayList<Serializable> boids = new ArrayList<Serializable>();
		if(InvoiceInfos.size()>0){
			for(int i=0;i<InvoiceInfos.size();i++){
				/**
				 * 保存数据插入的id
				 */
				if(InvoiceInfos.get(i).getIsBorrowed()==0){ 
					id = this.invoiceInfoDao.saveInvo(InvoiceInfos.get(i));
					myids.add(id);
				}
				else{
					id=this.invoiceInfoDao.saveInvo(InvoiceInfos.get(i));
					boids.add(id);
				}
			}
		}
		/**
		 * 插入DriverWorkload表
		 */
		
		for(int i=0;i<drivers.size();i++){
			InvoiceInfo temp = (InvoiceInfo) invoiceInfoDao.get(InvoiceInfo.class, myids.get(i));
			String driverStr = drivers.get(i);//每一行Cell就是一个drivers的i
			String[] str = driverStr.split(",");//自己的车多个驾驶员分割
			for(int j=0;j<str.length;j++){
				DriverWorkloadId driverWorkID = new DriverWorkloadId(); //主键
				DriverWorkload driverWork = new DriverWorkload();
				
				String name = str[j]; //获得逗号分隔的驾驶员name
				List<DriverInfo> ds = this.driverDao.loadDriverByName(name);
				DriverInfo d = null;
				if(ds.size()>0){ //获得该name对应的驾驶员
					d = ds.get(0);
				}
				/**
				 * 每有一个驾驶员就对应一条DriverWorkload数据 
				 * 同一个cell行下的DriverWorkload的InvoiceInfo相同
				 */
				driverWorkID.setDriverInfo(d);      
				driverWorkID.setInvoiceInfo(temp); 
				driverWork.setId(driverWorkID);
				this.driverWorkloadDao.save(driverWork);
			}
		}
		/**
		 * 插入BorrowTruckInfo表
		 */
		for(int m=0;m<borrowTrucks.size();m++){
			InvoiceInfo temp = (InvoiceInfo) invoiceInfoDao.get(InvoiceInfo.class, boids.get(m));
			borrowTrucks.get(m).setInvoiceInfo(temp);
			BorrowTruckInfo bt = borrowTrucks.get(m);
			this.borrowTruckDao.save(bt);
		}
		return true;
	}
	/**
	 * 根据id加载订单信息
	 * @param invoiceOrderId
	 * @return
	 * @throws Exception
	 */
	public InvoiceOrder loadInvoiceOrderById(String invoiceOrderId)throws Exception{
		return (InvoiceOrder) this.invoiceOrderDao.get(InvoiceOrder.class, invoiceOrderId);
	}
	/**
	 * 修改订单信息
	 * @param invoiceOrder
	 * @return
	 * @throws Exception
	 */
	public boolean updateInvoiceOrder(InvoiceOrder invoiceOrder,String falg,ArrayList<InvoiceInfo> invoiceInfos,ChargeRecord chargeRecordS,ChargeRecord chargeRecordR,ArrayList<String> drivers,ArrayList<BorrowTruckInfo> borrowTrucks)throws Exception{
		if(falg!=null&&falg.equals("PaiFa")){
			this.invoiceOrderDao.update(invoiceOrder);
			return true;
		}
		/**
		 * 修改订单信息
		 */
		this.invoiceOrderDao.update(invoiceOrder);
		/**
		 * 修改运费信息
		 */
		if(chargeRecordS!=null){
			this.chargeRecordDao.update(chargeRecordS);
		}
		if(chargeRecordR!=null){
			this.chargeRecordDao.update(chargeRecordR);
		}
		/**
		 * 删除然后新增invoiceInfo表及其子表信息
		 */
		Serializable id;
		ArrayList<String> myids = new ArrayList<String>(); //自己的车
		ArrayList<String> boids = new ArrayList<String>();
		List<InvoiceInfo> invoDeles = this.invoiceInfoDao.queryOrder(invoiceOrder.getOrderId());
		if(invoDeles.size()>0){
			for(int i=0;i<invoDeles.size();i++){
				/**
				 * 保存获取的数据id
				 */
				if(invoDeles.get(i).getIsBorrowed()==0){ 
					id = this.invoiceInfoDao.saveInvo(invoDeles.get(i));
					myids.add(id.toString());
				}
				else{
					id=this.invoiceInfoDao.saveInvo(invoDeles.get(i));
					boids.add(id.toString());
				}
			}
		}
		/**
		 * 删除DriverWorkload表
		 */
		
		for(int i=0;i<myids.size();i++){
			this.driverWorkloadDao.deleteOrder(myids.get(i));
			this.invoiceInfoDao.delete(InvoiceInfo.class, myids.get(i));
		}
		/**
		 * 删除BorrowTruckInfo表
		 */
		for(int m=0;m<boids.size();m++){
			this.borrowTruckDao.deleteOrder(boids.get(m));
			this.invoiceInfoDao.delete(InvoiceInfo.class, boids.get(m));
		}
		/**
		 * 插入
		 */
		myids.clear(); //移除表中所有元素，为下面插入重新保存元素
		boids.clear();
		if(invoiceInfos.size()>0){
			for(int i=0;i<invoiceInfos.size();i++){
				/**
				 * 保存数据插入的id
				 */
				if(invoiceInfos.get(i).getIsBorrowed()==0){ 
					id = this.invoiceInfoDao.saveInvo(invoiceInfos.get(i));
					myids.add(id.toString());
				}
				else{
					id=this.invoiceInfoDao.saveInvo(invoiceInfos.get(i));
					boids.add(id.toString());
				}
			}
		}
		for(int i=0;i<drivers.size();i++){
			InvoiceInfo temp = (InvoiceInfo) invoiceInfoDao.get(InvoiceInfo.class, myids.get(i));
			String driverStr = drivers.get(i);//每一行Cell就是一个drivers的i
			String[] str = driverStr.split(",");//自己的车多个驾驶员分割
			for(int j=0;j<str.length;j++){
				DriverWorkloadId driverWorkID = new DriverWorkloadId(); //主键
				DriverWorkload driverWork = new DriverWorkload();
				
				String name = str[j]; //获得逗号分隔的驾驶员name
				List<DriverInfo> ds = this.driverDao.loadDriverByName(name);
				DriverInfo d = null;
				if(ds.size()>0){ //获得该name对应的驾驶员
					d = ds.get(0);
				}
				/**
				 * 每有一个驾驶员就对应一条DriverWorkload数据 
				 * 同一个cell行下的DriverWorkload的InvoiceInfo相同
				 */
				driverWorkID.setDriverInfo(d);      
				driverWorkID.setInvoiceInfo(temp); 
				driverWork.setId(driverWorkID);
				this.driverWorkloadDao.save(driverWork);
			}
		}
		for(int m=0;m<borrowTrucks.size();m++){
			InvoiceInfo temp = (InvoiceInfo) invoiceInfoDao.get(InvoiceInfo.class, boids.get(m));
			borrowTrucks.get(m).setInvoiceInfo(temp);
			BorrowTruckInfo bt = borrowTrucks.get(m);
			this.borrowTruckDao.save(bt);
		}
		return true;
	}
	/**
	 * 删除订单信息
	 * @param invoiceOrderId
	 * @return
	 * @throws Exception
	 */
	public boolean deleteInvoiceOrder(String invoiceOrderId)throws Exception{
		String[] delId = invoiceOrderId.split(",");
		for(int i=0;i<delId.length;i++){
			String id = delId[i];
			/**
			 * 删除chargeRecord表
			 */
			this.chargeRecordDao.deleteOrder(id);
			/**
			 * 根据orderId查询InvoiceInfo表
			 */
			List<InvoiceInfo> temps = this.invoiceInfoDao.queryOrder(id);
			for(int j=0;j<temps.size();j++){
				String invoId = temps.get(j).getId();
				this.borrowTruckDao.deleteOrder(invoId);
				this.driverWorkloadDao.deleteOrder(invoId);
				this.invoiceInfoDao.delete(InvoiceInfo.class, invoId);
			}
			this.invoiceOrderDao.delete(InvoiceOrder.class, id);
		}
		return true;
	}
	/**
	 * 根据格式化的日期查询订单ID
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public List<String> queryOrderId(String date)throws Exception{
		return this.invoiceOrderDao.queryOrderId(date);
	}
	/**
	 * 查询需要派单数据的条数
	 */
	public int loadInvosOrderIdCount(String order)throws Exception{
		return this.invoiceOrderDao.loadInvosOrderIdCount("");
	}
	/**
	 * get set
	 * @return
	 */
	public InvoiceOrderDao getInvoiceOrderDao() {
		return invoiceOrderDao;
	}
	public void setInvoiceOrderDao(InvoiceOrderDao invoiceOrderDao) {
		this.invoiceOrderDao = invoiceOrderDao;
	}
	public ChargeRecordDao getChargeRecordDao() {
		return chargeRecordDao;
	}
	public void setChargeRecordDao(ChargeRecordDao chargeRecordDao) {
		this.chargeRecordDao = chargeRecordDao;
	}
	public InvoiceInfoDao getInvoiceInfoDao() {
		return invoiceInfoDao;
	}
	public void setInvoiceInfoDao(InvoiceInfoDao invoiceInfoDao) {
		this.invoiceInfoDao = invoiceInfoDao;
	}
	public DriverWorkloadDao getDriverWorkloadDao() {
		return driverWorkloadDao;
	}
	public void setDriverWorkloadDao(DriverWorkloadDao driverWorkloadDao) {
		this.driverWorkloadDao = driverWorkloadDao;
	}
	public DriverDao getDriverDao() {
		return driverDao;
	}
	public void setDriverDao(DriverDao driverDao) {
		this.driverDao = driverDao;
	}
	public BorrowTruckDao getBorrowTruckDao() {
		return borrowTruckDao;
	}
	public void setBorrowTruckDao(BorrowTruckDao borrowTruckDao) {
		this.borrowTruckDao = borrowTruckDao;
	}
	
}
